import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('../components/Welcome.vue'),
        meta: {
          title: 'Welcome'
        }
      },
      {
        path: '/users',
        component: () => import('../components/user/Users.vue'),
        meta: {
          title: '用户列表'
        }
      },
      {
        path: '/rights',
        component: () => import('../components/Power/Rights.vue'),
        meta: { title: '权限列表' }
      },
      {
        path: '/roles',
        component: () => import('../components/Power/Roles.vue'),
        meta: { title: '角色列表' }
      },
      {
        path: '/categories',
        component: () => import('../components/goods/Cate.vue'),
        meta: { title: '商品分类' }
      },
      {
        path: '/params',
        name: 'routeName',
        component: () => import('../components/goods/Params.vue'),
        meta: { title: '分类参数' }

      },
    ],
    meta: { title: '首页' }
  },
  {
    path: '/',
    redirect: '/login',
    meta: {
      title: '登录'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
